package com.legal.controller.chat;

import com.legal.service.chat.FileService;
import com.legal.utils.UserContext;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.client.advisor.AbstractChatMemoryAdvisor;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import reactor.core.publisher.Flux;

@RequiredArgsConstructor
@RestController
@RequestMapping("/ai/media")
@Slf4j
public class MediaChatController {
    private final ChatClient mediaClient;
    private final FileService fileService;


    //多模态实现：
    @PostMapping (value = "/testFile", produces = "text/html;charset=utf-8")
    public String chatFile(
          /*  @RequestParam("userNo") String userNo,
            @RequestParam("type") String type,
            @RequestParam("prompt") String prompt,
            @RequestParam("chatId") String chatId,*/
            @RequestParam(value = "files") MultipartFile file) {
        String s = fileService.ParseFile(file);
       return s;
    }

    //多模态实现：
    @PostMapping (value = "/test", produces = "text/html;charset=utf-8")
    public Flux<String> chat(
            @RequestParam("userNo") String userNo,
            @RequestParam("type") String type,
            @RequestParam("prompt") String prompt,
            @RequestParam("chatId") String chatId,
            @RequestParam(value = "files", required = false) MultipartFile file) {
        String s = fileService.ParseFile(file);
        UserContext.setContext(userNo, chatId);
        String fullPrompt = String.format("用户问题：%s \n end\n用户编号：%s\n会话ID：%s", prompt, userNo, chatId);
        return mediaClient.prompt()
                .user(fullPrompt)
                .advisors(advisorSpec -> advisorSpec.param(AbstractChatMemoryAdvisor.CHAT_MEMORY_CONVERSATION_ID_KEY, chatId))
                .stream() //表示流式调用，会一直返回数据，直到数据结束
                .content();

    }




}
